package com.genexus.search;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import com.genexus.internet.StringCollection;

import org.apache.lucene.document.DateField;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import com.genexus.internet.IGxJSONAble;
import java.io.Serializable;
import json.org.json.JSONObject;
import json.org.json.IJsonFormattable;
import json.org.json.JSONException;

public class SearchResultItem implements ISearchResultItem, Serializable, IGxJSONAble
{

	private Document m_document;
	private float m_score;
	private JSONObject _Properties;

	public SearchResultItem() {
		_Properties = new JSONObject();
	}

	protected SearchResultItem(Document doc, float score)
	{
		_Properties = new JSONObject();
		m_document = doc;
		m_score = score;
	}
	public JSONObject GetProperties()
	{
		return _Properties;
	}
	public void Clear()
	{
		_Properties.clear();
	}

	public String getId()
	{
		return m_document.getField(IndexRecord.URIFIELD).stringValue();
	}

	public String getViewer()
	{
		return m_document.getField(IndexRecord.VIEWERFIELD).stringValue();
	}

	public String getTitle()
	{
		return m_document.getField(IndexRecord.TITLEFIELD).stringValue();
	}

	public String getMatch()
	{
		return m_document.getField(IndexRecord.MATCHFIELD).stringValue();
	}

	public void setMatch(String match){
		Searcher.getInstance().setMatch(m_document, match);
	}

	public String getFound()
	{
		//Searcher.getInstance().getFound(m_document, m_document.getField(IndexRecord.MATCHFIELD).stringValue(), m_document.getField(IndexRecord.QUERYFIELD).stringValue());
		return m_document.getField(IndexRecord.FOUNDFIELD).stringValue();
	}

	public String getDone()
	{
		return m_document.getField(IndexRecord.DONEFIELD).stringValue();
	}

	public String getQuery()
	{
		return m_document.getField(IndexRecord.QUERYFIELD).stringValue();
	}

	public void setQuery(String value){

		m_document.removeField(IndexRecord.QUERYFIELD);
		m_document.add(new Field(IndexRecord.QUERYFIELD, value, Field.Store.YES, Field.Index.UN_TOKENIZED));
		Searcher.getInstance().setMatchAndFound(m_document, value);
	}

	public String getType()
	{
		return m_document.getField(IndexRecord.ENTITYFIELD).stringValue();
	}

	public Date getTimestamp()
	{
		return DateField.stringToDate(m_document.getField(IndexRecord.TIMESTAMPFIELD).stringValue());
	}

	public float getScore()
	{
		return m_score;
	}

	public StringCollection getKey()
	{
		List fields = m_document.getFields();
		int size = fields.size();
		StringCollection keys = new StringCollection();
		for (int i = 0; i < size; i++)
		{
			Field f = (Field)fields.get(i);
			if (f.name().startsWith(IndexRecord.KEYFIELDPREFIX))
				keys.add(f.stringValue());
		}
		return keys;
	}
	public String ToJavascriptSource()
	{
		return GetJSONObject().toString();
	}
	public Object GetJSONObject()
	{
		tojson();
		return _Properties;
	}

	public void FromJSONObject(IJsonFormattable obj) {
	}
	public void AddObjectProperty(String name, Object prop)
	{
		try
		{
			_Properties.put(name, prop);
		}
		catch (JSONException e) {}
	}
	public void tojson()
	{
		AddObjectProperty("Id", getId());
		AddObjectProperty("Viewer", getViewer());
		AddObjectProperty("Title", getTitle());
		AddObjectProperty("Type", getType());
		AddObjectProperty("Score", new Float(getScore()));
		AddObjectProperty("Timestamp", getTimestamp());
		AddObjectProperty("Match", getMatch());
		AddObjectProperty("Found", getFound());
		AddObjectProperty("Done", getDone());
		AddObjectProperty("Query", getQuery());
	}

}
